package com.itinsys.enma.web;


import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itinsys.enma.domain.Resource;
import com.itinsys.enma.domain.response.ResourceResponse;
import com.itinsys.enma.manager.ResourceManager;
import com.itinsys.enma.manager.ResponseManager;

/**
 * @author rperez
 *
 */

@Controller
@RequestMapping({ "/resource" })
public class ResourceFormController extends FormController {
	protected final Log logger = LogFactory.getLog("controller");
	
	@Autowired
	private ResourceManager resourceManager;

	@Autowired
	private ResponseManager responseManager;
	
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ResponseBody
	public ResourceResponse getAllResources() {
		return resourceManager.getAllResources();
	}

	@RequestMapping(value = { "/add" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ResourceResponse addResource(@RequestBody Map<String,String> resourceDTO,HttpServletResponse response) {
		final String description = (String) resourceDTO.get("description");
		Resource newResource = new Resource();
		newResource.setDescription(description);
		return this.resourceManager.addResource(newResource);
	}

	@RequestMapping(value = { "/edit" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ResourceResponse editResource(@RequestBody Map<String,String> resourceDTO,HttpServletResponse response) {
		Resource resource = new Resource();
		resource.setId(Integer.parseInt(resourceDTO.get("id")));
		resource.setDescription(resourceDTO.get("description"));
		return this.resourceManager.updateResource(resource);
	}
	

	@RequestMapping(value = { "/delete" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
	@ResponseBody
	public ResourceResponse deleteResource(@RequestBody Map<String,String> resourceDTO,HttpServletResponse response) {
		Resource resource = new Resource();
		resource.setId(Integer.parseInt(resourceDTO.get("id")));
		return this.resourceManager.deleteResource(resource);
	}
	


	
}
